<?php
// +-------------------------------------------------------------------
// |
// +-------------------------------------------------------------------
// | Copyright (c) 2009-2016 All rights reserved.
// +-------------------------------------------------------------------
namespace Service\Api\V1;

use Think\Model;

class TrainList
{
    public $login = false; // 是否需要登录才能访问该接口
    public $title = "视觉训练列表";
    public $group = '视觉训练';
    public $desc = "";

    public $input = [
        'page_num' => 'int;label=分页;comment=默认为1',
        'page_size' => 'int;label=分页条数;comment=默认为5',
    ];

    public $output = [
        'page_num' => 'label=当前页码;',
        'page_size' => 'label=每页条数;',
        'page_count' => 'label=总条数;',
        'page_total' => 'label=总页数;',
        'list' => [
            [
                'id' => 'label=id;',
                'title' => 'label=分享标题;',
                'supplement1' => 'label=标题下补充1;',
                'supplement2' => 'label=标题下补充2;',
                'sound' => 'label=声音;',
                'pic' => 'label=图片;',
                'sharepic' => 'label=分享图片;',
                'count' => 'label=训练达成人数;',
            ]
        ],
        'total' => [
            'time_min' => 'label=训练了多少分钟;',
            'num' => 'label=完成次数',
            'days' => 'label=累计天数',
        ],
    ];



    public function run($param, $uid)
    {
        $page_num = $param['page_num'] ?: 1;
        $page_size = $param['page_size'] ?: 5;
        list($list, $page_count, $page_total) = OE('User')->trainList($uid, $page_num, $page_size);
        foreach($list as &$item){
            $item['sharepic'] = get_cover($item['sharepic'], 'url');
        }
        $num = M('eyes_train_log')->where(['userid' => $uid])->count();
        $time_min = M('eyes_train_log')->where(['userid' => $uid])->sum('playtime');
        $sql = "SELECT * FROM `k_eyes_train_log` where userid={$uid} group by stardate";
        $model = new Model();
        $ret = $model->query($sql);
        $days = count($ret);
        return [
            'page_num' => $page_num,
            'page_size' => $page_size,
            'page_count' => $page_count,
            'page_total' => $page_total,
            'list' => $list,
            'total' => [
                'time_min' => floor($time_min/60)?floor($time_min/60):0,
                'num' => $num?$num:0,
                'days' => $days,
            ]
        ];
    }
}
